home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / tex / lametex_.z / lametex_ / lametex / src / Token.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  1.8 KB  |  68 lines

  1. /* Token.C
  2.  *
  3.  * The text to be parsed is broken into fundamental units called tokens.
  4.  * To parse the LaTeX files, the program interprets and handles these tokens.
  5.  *
  6.  * Copyright 1992 Jonathan Monsarrat. Permission given to freely distribute,
  7.  * edit and use as long as this copyright statement remains intact.
  8.  *
  9.  */
  10.  
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "Operator.h"
  14. #include "Global.h"
  15. #include "Document.h"
  16.  
  17. /* Gets a new token from the last line read from a file. If no such line 
  18.  * exists, it gets a new line from the open file. If no file is open, it
  19.  * opens a new file. If no more files are left to open, returns failure. */
  20. Token::Token()
  21. {
  22.    _valid = FALSE;
  23.    Global::files->get_token(*this);    // Reads a new token from a file
  24. }
  25.  
  26. /* Deal correctly with this new token. */
  27. void Token::handle()
  28. {
  29. //   cerr << "Token: " << _text << endl;
  30.    // Look up the appropriate operator from an array of operators.
  31.    Operator *op = Operator::get_operator(_text);
  32.    if(op)
  33.       do
  34.      op->execute();       // Execute the operator.
  35.       while((++op)->isvalid() && op->match(_text));
  36.    else {
  37.       if(_text[0] == '\\' &&  // Found unknown command and not in a comment
  38.          !Stack::get(Environment::PDocument, Document::Comment, "")) {
  39.          char message[MAXSTRING];  // It's a LaTeX command I don't understand!
  40.      sprintf(message,"Skipping unknown LaTeX command %s", _text);
  41.          Global::files->warning(message);
  42.          return;
  43.       }
  44.       Operator::plaintext(_text);  // If no operator, it must be plain text.
  45.    }
  46. }
  47.  
  48. int Token::isvalid()
  49. {
  50.    return(_valid);
  51. }
  52.  
  53. void Token::make_text(char *token_text)
  54. {
  55.    strcpy(_text,token_text);
  56.    _valid = TRUE;
  57. }
  58.  
  59. int Token::match(char *str)
  60. {
  61.    return !strcmp(str,_text);
  62. }
  63.  
  64. char *Token::get_text()
  65. {
  66.    return _text;
  67. }
  68.